﻿Imports System.Xml


Module Process
    'Some Variables :)
    Public NxtPrayer As String = "none"
    Dim month As Integer = Now.Month
    Dim day As Integer = Now.Day
    'Remaining time to...
    Public DF As Integer 'Remaining time to Fajr in seconds.
    Public DS As Integer 'Remaining time to Shruq in seconds.
    Public DD As Integer 'Remaining time to Dhur............
    Public DA As Integer '................. Asr ............
    Public DM As Integer '................. Maghrib.........
    Public DI As Integer '................. Isha ...........
    'Maxium Times
    Public MF As Integer 'Time Difference between Isha and Fajr
    Public MS As Integer 'Time Difference between Fajr and Shruq
    Public MD As Integer
    Public MA As Integer
    Public MM As Integer
    Public MI As Integer
    'The Prayers with default time
    Public Fajr As String = "6:02 AM"
    Public Shruq As String = "7:21 AM"
    Public Dhr As String = "12:16 PM"
    Public Asr As String = "2:45 PM"
    Public Maghrib As String = "5:06 PM"
    Public Isha As String = "6:21 PM"


    Public Sub Gtdff()
        GetPrayerTimes()
        '''''''''''''''''''''''''''FAJR'''''''''''''''''''''''

        Dim fajrD As Date = Date.Parse(Fajr) 'Get today's Fajr time, then parse it to date format (because it's saved as string)
        Dim newTimeF As Date = Now 'Current time
        'Get the difference between now and next fajr.
        Dim differenceInSecondsF As Long = DateDiff(DateInterval.Second, newTimeF, fajrD)
        DF = differenceInSecondsF
        '''''''''''''''''SHRUQ'''''''''''''''''''''''''''
        Dim ShruqD As Date = Date.Parse(Shruq) 'Get today's Fajr time, then parse it to date format (because it's saved as string)
        Dim newTimeS As Date = Now 'Current time
        'Get the difference between now and next fajr.
        Dim differenceInSecondsS As Long = DateDiff(DateInterval.Second, newTimeF, fajrD)
        DS = differenceInSecondsS
        ''''''''''''''''DHUR'''''''''''''''''''
        Dim DhrD As Date = Date.Parse(Dhr)
        Dim newTimeD As Date = Now
        Dim differenceInSecondsD As Long = DateDiff(DateInterval.Second, newTimeD, DhrD)
        DD = differenceInSecondsD

        ''''''''''''''''ASR'''''''''''''''''

        Dim AsrD As Date = Date.Parse(Asr)
        Dim newTimeA As Date = Now
        Dim differenceInSecondsA As Long = DateDiff(DateInterval.Second, newTimeA, AsrD)
        DA = differenceInSecondsA

        ''''''''''''''''MAGHRIB'''''''''''''''''

        Dim MaghribD As Date = Date.Parse(Maghrib)
        Dim newTimeM As Date = Now
        Dim differenceInSecondsM As Long = DateDiff(DateInterval.Second, newTimeM, MaghribD)
        DM = differenceInSecondsM

        '''''''''''''''ISHA'''''''''''''''''''''''
        Dim IshaD As Date = Date.Parse(Isha)
        Dim newTimeI As Date = Now
        Dim differenceInSecondsI As Long = DateDiff(DateInterval.Second, newTimeI, IshaD)
        DI = differenceInSecondsI

   

        '''''''''''''Max Fajr'''''''''''''''''''''''
        'Get the difference between Isha and fajr.
        Dim differenceInSecondsFM As Long = DateDiff(DateInterval.Second, IshaD, fajrD)
        MF = differenceInSecondsFM
        '''''''''''''Max Shruq'''''''''''''''''''''''
        'Get the difference between Fajr and Shruq.
        Dim differenceInSecondsSM As Long = DateDiff(DateInterval.Second, fajrD, ShruqD)
        MS = differenceInSecondsSM
        '''''''''''''Max Dhr'''''''''''''''''''''''
        'Get the difference between Fajr and Dhr.
        Dim differenceInSecondsDM As Long = DateDiff(DateInterval.Second, fajrD, DhrD)
        MD = differenceInSecondsDM
        '''''''''''''Max Asr'''''''''''''''''''''''
        'Get the difference between Dhr and Asr.
        Dim differenceInSecondsAM As Long = DateDiff(DateInterval.Second, DhrD, AsrD)
        MA = differenceInSecondsAM
        '''''''''''''Max Maghrib'''''''''''''''''''''''
        'Get the difference between Asr and Maghrib.
        Dim differenceInSecondsMM As Long = DateDiff(DateInterval.Second, AsrD, MaghribD)
        MM = differenceInSecondsMM
        '''''''''''''Max Isha'''''''''''''''''''''''
        'Get the difference between Maghrib and Isha.
        Dim differenceInSecondsIM As Long = DateDiff(DateInterval.Second, MaghribD, IshaD)
        MI = differenceInSecondsIM
   
    End Sub

    Public Sub GetPrayerTimes()
        Dim DataFile As New INIFile
        DataFile.Load(Application.StartupPath & "\Data\PrayerTimes\Erbil.ini")
        Dim Today As INIFile.IniSection = DataFile.GetSection("04;30")
        Fajr = Today.GetKey("Fajr").GetValue
        Shruq = Today.GetKey("Shruq").GetValue
        Dhr = Today.GetKey("Dhur").GetValue
        Asr = Today.GetKey("Asr").GetValue
        Maghrib = Today.GetKey("Maghrib").GetValue
        Isha = Today.GetKey("Isha").GetValue

    End Sub

    Public Sub GetNextPrayer()

        Gtdff() 'Get the remaining time to the prayers

        'Do some calculations :D
        If DF > 0 Then 'IF Time difference between Fajr and current moment was greater than 0 seconds
            If DF < DD Then 'If remaining time of Fajr was less than the remaining time of Dhur
                NxtPrayer = "Fajr" 'Then the next prayer is Fajr
            End If
        End If

        If DD > 0 Then  'Same story...
            If DD < DA Then
                If DF <= 0 Then
                    NxtPrayer = "Dhr"
                End If
            End If
        End If

        If DA > 0 Then 'Same story...
            If DA < DM Then
                If DD <= 0 Then
                    NxtPrayer = "Asr"
                End If
            End If
        End If

        If DM > 0 Then 'Same story...
            If DM < DI Then
                If DA <= 0 Then
                    NxtPrayer = "Maghrib"
                End If
            End If
        End If


        If DI > 0 Then 'Same story...
            If DI >= DF Then
                If DM <= 0 Then
                    NxtPrayer = "Isha"
                End If
            End If
        End If

        If DI < 0 Then 'After Time difference of Isha becomes less than 0 seconds, change back the next prayer to Fajr.
            NxtPrayer = "Fajr"
        End If

    End Sub
    Function ChangeFormat(ByVal number As Integer)
        Dim newnumber As String
        If number < 10 Then
            newnumber = "0" & number
        Else
            newnumber = number
        End If
        Return newnumber
    End Function
End Module
